package com.util.spliterator;

import java.util.Comparator;
import java.util.Spliterator;
import java.util.function.Consumer;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * @Description 数字计数分割，第一个Spliterator迭代器
 * @Author away
 * @Version V1.0.0
 * @Since 1.0
 * @Date 2019/11/4
 */
public class NumCounterSpliterator implements Spliterator<Character> {
    private char[] str;
    private int currentChar = 0;
    private int end = Integer.MAX_VALUE;
    private boolean canSplit = true;

    public NumCounterSpliterator(int currentChar, int end, char[] str, boolean canSplit) {
        this.str = str;
        this.currentChar = currentChar;
        this.canSplit = canSplit;
        this.end = end;
        StringBuilder sb = new StringBuilder();
        for(int i =currentChar;i<=end&& i< str.length;i++){
            sb.append(str[i]);
        }
        System.out.println(Thread.currentThread().getName()+" splite  "+ sb);
    }
//5
    @Override
    public boolean tryAdvance(Consumer<? super Character> action) {
        if(currentChar<=end&&currentChar<str.length-1) {
            action.accept(str[currentChar]);
            currentChar++;
        }
        return currentChar <= end&&currentChar<str.length-1;
    }


    //4
    @Override
    public Spliterator<Character> trySplit() {
        int i = currentChar;
        int currentCharOld = currentChar;
        for (; canSplit && i <= end; ++i) {
            if (!Character.isDigit(str[i])) {
                int splitBeforeEnd = end;
//                canSplit = false;
                if (i + 1 <= splitBeforeEnd) {
                    currentChar = i + 1;
                    return new NumCounterSpliterator(currentCharOld, i, str, true);
                } else {
                    return null;
                }
            }
        }

        canSplit = false;
        return null;
    }
//3
    @Override
    public long estimateSize() {
        return end - currentChar ;/*Long.MAX_VALUE*/
    }

    @Override
    public Comparator<? super Character> getComparator() {
        return null;
    }
//1
    @Override
    public int characteristics() {
        return ORDERED | SIZED | SUBSIZED | NONNULL | IMMUTABLE /*|SORTED*/;
    }


    public static void main(String[] args) {
        int a= 12+3+21+34+45+3+34+8+3+1+213+55;//432 419
        System.out.println("实际结果 : " +a);
        String arr = "12%3 21sdas s34d dfsdz45 R3 jo34 sjkf8 3$1P 213ikflsd fdg55 kfd";
        System.out.println("数组长度 " + arr.length());

        Spliterator<Character> spliterator = new NumCounterSpliterator(0,arr.length(),arr.toCharArray(),true);
// 传入true表示是并行流
        Stream<Character> parallelStream = StreamSupport.stream(spliterator, true);
//        parallelStream.forEach(s->System.out.println(s));
//        parallelStream.forEach(s->System.out.println(s));


        System.out.println("parallel total: " + countNum(parallelStream));
    }
//2
    private static int countNum(Stream<Character> stream){
        NumCounter numCounter = stream.reduce(new NumCounter(0, 0, false), NumCounter::accumulate, NumCounter::combine);
        return numCounter.getSum();
    }





}
